package com.shopping.mall.core.module.goodData.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.goodData.entity.GoodData;
import com.shopping.mall.core.module.goodData.entity.GoodDataQueryVO;
import com.shopping.mall.core.module.goodData.service.GoodDataService;
import com.shopping.mall.core.module.saleData.entity.SaleData;
import com.shopping.mall.core.module.saleData.entity.SaleDataQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Api(tags = "GoodData")
@RestController
@RequestMapping("/goodData")
public class GoodDataController {
    @Autowired
    private GoodDataService goodDataService;

    @UnLogin
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<GoodData>> getGoodDataPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        GoodDataQueryVO goodDataQueryVO) {
        IPage<GoodData> goodDataPage=goodDataService.getGoodDataPage(current,size,goodDataQueryVO);
        return Result.OK(goodDataPage);
    }

    @ApiOperation("修改")
//    @PutMapping
    public Result<GoodData> update(@Validated(GoodData.update.class) @RequestBody GoodData goodData) {
        goodDataService.updateById(goodData);
        return Result.OK();
    }

    @UnLogin
    @ApiOperation("新增商品访问（商品、规格ID）")
    @PostMapping
    public Result<Object> addVisit(@Validated(GoodData.insert.class) @RequestBody GoodData goodData) {
        goodDataService.addVisit(goodData);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<GoodData> getById(@RequestParam String id){
        GoodData goodData=goodDataService.getById(id);
        return Result.OK(goodData);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<GoodData>> getAll(GoodDataQueryVO goodDataQueryVO){
        List<GoodData> goodDataList=goodDataService.getAll(goodDataQueryVO);
        return Result.OK(goodDataList);
    }

    @ApiOperation("通过id删除")
//    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return goodDataService.deleteById(id);
    }

    @ApiOperation("批量删除")
//    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return goodDataService.batchDelete(ids);
    }

    @UnLogin
    @ApiOperation("导出数据")
    @GetMapping("/data/excel")
    public void getExcel(GoodDataQueryVO goodDataQueryVO, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("商品访问量", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<GoodData> data=goodDataService.getAll(goodDataQueryVO);
        EasyExcel.write(response.getOutputStream(), GoodData.class).sheet("sheet1").doWrite(data);
    }
}
